package com.jdew.thkj.fragment;

import android.graphics.Bitmap;
import android.webkit.ConsoleMessage;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.jdew.thkj.R;
import com.jdew.thkj.base.BaseFragment;

import butterknife.Bind;

/**
 * Created by yangzai on 2018/3/28 0028.
 */

public class SuperCoachFragment extends BaseFragment {
    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.webview)
    WebView webview;
    private WebView mWebView;

    @Override
    protected int getContentViewId() {
        return R.layout.fragment_super_coach;
    }

    @Override
    protected void onCreateViewT() {
    }

    @Override
    public void initData() {
        initWeb(webview);
    }

    public static SuperCoachFragment newInstance() {
        SuperCoachFragment fragment = new SuperCoachFragment();
        return fragment;
    }

    protected void initWeb(WebView webView) {
        title.setText("超级教练");
        mWebView = webView;
        WebSettings webSettings = webView.getSettings();
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        mWebView.setInitialScale(5);
        webSettings.setSupportZoom(true);
        webSettings.setBuiltInZoomControls(true);
        webSettings.setDisplayZoomControls(false);
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setCacheMode(WebSettings.LOAD_DEFAULT); //设置 缓存模式
        webSettings.setDomStorageEnabled(true);
        webSettings.setDatabaseEnabled(true);
        webSettings.setAppCacheEnabled(true);
        loadUrl("http://www.taohuikeji.com/h5Web/app_home/Index.html");
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
            }

            @Override
            public void onPageFinished(WebView view, String url) {

            }
        });

        mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {

            }

            @Override
            public void onConsoleMessage(String message, int lineNumber, String sourceID) {
            }

            @Override
            public boolean onConsoleMessage(ConsoleMessage cm) {
                return true;
            }
        });
    }

    protected void loadUrl(String url) {
        mWebView.loadUrl(url);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (null != mWebView) {
            mWebView.stopLoading();
            mWebView.getSettings().setJavaScriptEnabled(false);
            mWebView.clearHistory();
            mWebView.removeAllViews();
            mWebView.destroy();
        }
    }
}
